import allure
import requests
from common.config import *
from common.opmysql import *


# branchId='23141'
# branchId='174'

#猫酷会员接口
class TestMaoku():
    @allure.step('会员查询')
    def test_memberInfo(self,env,phone):
        dayu_url = getconf(env, "dayu_url")
        branchId=getconf(env,"branchId")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "platform-ryware-server/v1/ryware/dayu_03member/getOneInfoByPhone"
        # print (url)
        # phone = 13667347062
        # phone = 18221413114
        # phone = 18225076781
        # phone = 18721428902
        # phone = 13818335213
        data = {  "branchId": branchId,
                  "createTimestamp": 0,
                  "current": 0,
                  "deleted": "NO",
                  "modifiedTimestamp": 0,
                  "phone":phone,
                  "pageStart": -10,
                  "size": 10}
        r=requests.get(url,headers=headers,params=data)
        # print ("查会员",r.json())
        return (r.json()['data']['cardCode'])

    @allure.step('通过会员唯一编号发放券码')
    def test_codeAcquire(self,env,phone,branchId,couponId):
        dayu_url = getconf(env, "dayu_url")
        token=buslogin(env)
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "platform-ryware-server/v1/ryware/coupon/codeAcquire"
        CardCode = TestMaoku().test_memberInfo(env,phone)
        print (CardCode)
        # couponId=int(getconf(env, "maokucouponId"))
        sql='SELECT app_id FROM `buorg`.`wechat_branch_wechat` where branch_id="'+branchId+'"'
        appid=OperationDb(env).select_one(sql)
        data = {
            "requestNo":"xxxx",
            "clientAppId":appid,
            "couponId":couponId,
            "memberCardCode":CardCode,
            "quantity":1,
            "publishChannel":"ACITIVITY",
            "subChannelName":"yyyyyy"}
        r=requests.post(url,json = data,headers=headers,params={"Token":token})
        print ("发券",r.json())

    @allure.step('增加积分')
    def increasepoint(self,env,phone):
        dayu_url = getconf(env, "dayu_url")
        token=buslogin(env)
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "platform-ryware-server/v1/ryware/point/increase"
        CardCode = TestMaoku().test_memberInfo(env,phone)
        branchId = getconf(env, "branchId")
        data = {
            "cardCode":CardCode,
            "changePoint":10000,
            "pointSource":"MARKETING_ACT_GAME_DEDUCTION_POINT",
            "remark":"手动调用加积分"}
        r=requests.post(url,json = data,headers=headers,params={"branchId":branchId})
        print ("加积分",r.json())

    @allure.step('扣减积分')
    def test_deductionpoint(self,env,phone):
        dayu_url = getconf(env, "dayu_url")
        token=buslogin(env)
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "platform-ryware-server/v1/ryware/point/deduction"
        CardCode = TestMaoku().test_memberInfo(env,phone)
        branchId = getconf(env, "branchId")
        data = {
            "cardCode":CardCode,
            "changePoint":100,
            "pointSource":"MARKETING_ACT_GAME_DEDUCTION_POINT",
            "remark":"手动调用扣减积分"}
        r=requests.post(url,json = data,headers=headers,params={"branchId":branchId})
        print ("扣减积分",r.json())

    @allure.step('查询总积分')
    def queryTotalPoint(self,env,phone):
        dayu_url = getconf(env, "dayu_url")
        headers = {"Content-Type": "application/json"}
        url = dayu_url + "platform-ryware-server/v1/ryware/point/queryTotalPoint"
        CardCode = TestMaoku().test_memberInfo(env,phone)
        branchId = getconf(env, "branchId")
        data = {"cardCode":CardCode,"branchId":branchId}
        r=requests.get(url,json = data,headers=headers,params=data)
        print ("查总积分",r.json())
        return (r.json()['data'])

    @allure.step('查猫酷卡券')
    def test_querymallcoolist(self,env):
        dayu_url = getconf(env, "dayu_url")
        # token=buslogin(env)
        headers = {"Content-Type": "application/json"}
        # url = dayu_url + "platform-ryware-server/v1/ryware/coupon/mallcoo/list"
        url = "http://dayu.rongyi.com/" + "platform-ryware-server/v1/ryware/coupon/mallcoo/list"
        merchant = "huolicheng"
        branchId=3008
        # branchId = getconf(env, "branchId")
        data = {"merchant":merchant,"branchId":branchId}
        r=requests.get(url,json = data,headers=headers,params=data)
        print ("查猫酷卡券",r.json())

if __name__ == '__main__':
    # a=TestAiyings()increasepoint
    # a.test_codeAcquire("test")test_deductionpoint
    # queryTotalPoint
    # test_memberInfo
    # test_codeAcquire
    # command='pytest -s --env=test maoku_api.py::TestMaoku::test_querymallcoolist'
    # os.system(command)
    TestMaoku().test_codeAcquire("test", "18721428902",'23141',775)
    # TestMaoku().queryTotalPoint("test", "13131300337")
